package com.onlyphoto.onlymall.mvp.view.binder

import android.annotation.SuppressLint
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

import com.onlyphoto.onlymall.R
import com.bumptech.glide.Glide
import com.onlyphoto.onlymall.bean.ShopBean
import com.onlyphoto.onlymall.bean.request.UpdateShopBean
import com.onlyphoto.onlymall.events.GetStyleEvent
import com.onlyphoto.onlymall.mvp.view.activity.GoodsDetailsActivity
import com.onlyphoto.onlymall.utils.StringUtil
import com.onlyphoto.onlymall.widget.AmountView

import me.drakeet.multitype.ItemViewBinder
import org.greenrobot.eventbus.EventBus

/**
 * Created by gaochujia on 2019-07-09.
 */
class ShopBinder : ItemViewBinder<ShopBean, ShopBinder.ViewHolder>() {

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        val root = inflater.inflate(R.layout.item_shop, parent, false)
        return ViewHolder(root)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, shopBean: ShopBean, payloads: MutableList<Any>) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, shopBean)
        } else {
            holder.count?.amount = shopBean.buy_num
            if (StringUtil.isBlank(shopBean.style)) {
                holder.style?.text = shopBean.color + ";" +shopBean.size
            } else {
                holder.style?.text = shopBean.style
            }
        }
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, shopBean: ShopBean) {
        viewHolder.update(shopBean)
        viewHolder.count?.setOnAmountChangeListener{it ->
            val updateBean = UpdateShopBean(1, shopBean.id, shopBean.goods_id, it,
                    shopBean.state, shopBean.style, shopBean.size, shopBean.color)
            listener?.updateNum(updateBean)
            EventBus.getDefault().post(updateBean)
        }
        viewHolder.style?.setOnClickListener{
            EventBus.getDefault().post(GetStyleEvent(viewHolder.style?.text.toString(), shopBean.buy_num, shopBean.id, shopBean.goods_id))
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var cbProduct: CheckBox? = null
        var item: View = itemView
        var proImg: ImageView? = null
        var proTitle: TextView? = null
        var proPrice: TextView? = null
        var style: TextView? = null
        var count: AmountView? = null
        private var llGift: LinearLayout? = null
        var giftName: TextView? = null

        init {
            cbProduct = itemView.findViewById(R.id.cb_product)
            proImg = itemView.findViewById(R.id.pro_img)
            proTitle = itemView.findViewById(R.id.pro_name)
            proPrice = itemView.findViewById(R.id.pro_price)
            style = itemView.findViewById(R.id.style)
            count = itemView.findViewById(R.id.amount_view)
            llGift = itemView.findViewById(R.id.ll_gift)
            giftName = itemView.findViewById(R.id.gift_name)
            //为了条目不复用
            setIsRecyclable(false)
        }

        @SuppressLint("SetTextI18n")
        fun update(shopBean: ShopBean) {
            cbProduct?.isChecked = shopBean.state == 1
            cbProduct?.setOnClickListener {
                if (cbProduct?.isChecked!!) {
                    shopBean.state = 1
                } else {
                    shopBean.state = 2
                }
                EventBus.getDefault().post(shopBean)
            }
            Glide.with(proImg!!.context).load(shopBean.front_img).placeholder(R.mipmap.img_default).into(proImg!!)
            proTitle?.text = shopBean.title
            proPrice?.text = "¥ " + shopBean.price_now.toString()
            if (StringUtil.isBlank(shopBean.style)) {
                style?.text = shopBean.color + ";" +shopBean.size
            } else {
                style?.text = shopBean.style
            }
            count?.amount = shopBean.buy_num

            proTitle?.setOnClickListener{
                itemView.context.startActivity(Intent(itemView.context, GoodsDetailsActivity::class.java)
                        .putExtra("goodsId", shopBean.goods_id))
            }

            if (shopBean.gift_arr != null) {
                llGift?.visibility = View.VISIBLE
                giftName?.text = shopBean.gift_arr.title
            } else {
                llGift?.visibility = View.GONE
            }
        }

    }

    var listener: UpdateNumListener? = null

    fun setUpdateListener(listener: UpdateNumListener){
        this.listener = listener
    }

    interface UpdateNumListener{
        fun updateNum(bean: UpdateShopBean)
    }
}
